var file_id;
var oked=false;

var Utils = {
  /**
   * 伪文件选择框点击事件，触发真的文件选择框点击事件
   * @param {Object} that 文件选择框选择按钮对象
   */
  chooseFile: function (that) {
    $(that).parent().parent().children('input:first-child').click();
  },

  /**
   * 真文件选择框的change事件（此处将文件名显示到伪文件选择框）
   * @param {Object} that 真的文件选择框的 document 对象
   */
  /*afterChooseFile: function (that) {
    var viewInput = $(that).next('input');
    var name = that.files.length ? that.files[0].name : '';
    viewInput.val(name);
  },*/

  /**
   * 这里是yupeng自己新写 afterChooseFile 的，在真实框中内容改变之后就要检测是否相同并且上传，然后要返回id
   **/
  afterChooseFile: function (that) {
    var viewInput = $(that).next('input');
    var name = that.files.length ? that.files[0].name : '';
    viewInput.val(name);
    var files = $(that).prop('files');
    if (files.length) {
      // uploadFile(files[0]);
      oked=true;
    } else {
      $.notify({
        type: 'danger',
        message: gettext('Please select a file.')
      });
    }
  }
};

$(function () {
  $("#card-action-warning").hide();
  $("#submit").click(function () {
    if(oked){
      var files = $('#true-file').prop('files');
      var select_option = $("#select-option").val();
      // alert(select_option);
      uploadFile(files[0],select_option)
    }
    else{
      $.notify({
        type: 'danger',
        message: gettext('Please select a file.')
      });
    }
  });
  $("#card-action-warning").click(function () {
    $("#result").empty();
    $(this).hide();
  });
});


function uploadFile(file,select_option) {
  // alert(select_option);
  var formData = new FormData();
  formData.append('file', file);

  $('body').preloader({
    text: gettext('Uploading...'),
    percent: '0'
  });
  $.ajax({
    url: Urls.resolve('material_api_v1:performance'),
    // url: Urls.resolve(''),
    data: formData,
    //暂未传model值，即select_option
    processData: false,
    contentType: false,
    type: 'POST',
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            var complete = (e.loaded / e.total * 100 | 0);
            $('body').preloader('update', {
              percent: complete.toString(),
              text: gettext('Uploading...')
            });   // 更新loading进度
          }
        }, false);
      }
      return myXhr;
    }
  }).done(function (resp) { //前面的resp就是ajax回复的,这里是id
    // file_id = resp.data;
    // $("#true-file").data("file-id", file_id);
    $("#result").empty();
    var obj_title=
      '<div class="col-md-12 card-text-col">' +
      '<p class="prediction-type-text">'+resp.data.values[0]+'</p> </div>';
    $("#result").append(obj_title);
    for(var i=1;i<resp.data.keys.length;i++){
      var obj_content=
        '<div class="col-md-6 card-text-col">'+
        '<h3 class="section-title">'+resp.data.keys[i]+'</h3>'+
        '<p class="prediction-text">'+resp.data.values[i]+'</p> '+
        '</div>';
      $("#result").append(obj_content);
      $("#card-action-warning").show();
    }
  }).fail(function (data) {
    window._jqHandled = true;
  }).always(function () {
    $('body').preloader('remove');
  });
}

